﻿using Motions;
using Station;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tool;

namespace AutoMachine
{
    public partial class Frm_AxisPoint : Form
    {
        Tool.AutoResize FormResize = new Tool.AutoResize();//窗体缩放实例化

        private string SectionName = "";

        #region 窗体实例化
        /// <summary>
        /// 窗体对象实例化
        /// </summary>
        private static Frm_AxisPoint _instance;
        public static Frm_AxisPoint Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new Frm_AxisPoint();
                return _instance;
            }
        }
        #endregion

        public Frm_AxisPoint()
        {
            InitializeComponent();
            FormResize.SetAutoResize(this);//窗体缩放实例化

        }

        private void Frm_AxisPoint_Load(object sender, EventArgs e)
        {
            //TreeVie动态添加跟节点
            //添加跟节点
            this.treeView1.Nodes.Add("工位名称");
 
            //添加子节点  
            treeView1.Nodes[0].Nodes.Add("弹夹搬运工站");
            treeView1.Nodes[0].Nodes.Add("胶板拆分工站");
            treeView1.Nodes[0].Nodes.Add("胶板中转工站");
            treeView1.Nodes[0].Nodes.Add("中转定位工站");
            treeView1.Nodes[0].Nodes.Add("中转定位取料工站");
            treeView1.Nodes[0].Nodes.Add("飞拍1工站");
            treeView1.Nodes[0].Nodes.Add("中转搬运工站");
            treeView1.Nodes[0].Nodes.Add("双工位飞拍2工站");
            treeView1.Nodes[0].Nodes.Add("双工位飞拍3工站");
            treeView1.Nodes[0].Nodes.Add("飞拍后取料工站");
            treeView1.Nodes[0].Nodes.Add("分OKNG工站");
            treeView1.Nodes[0].Nodes.Add("OK放料工站");
            treeView1.Nodes[0].Nodes.Add("OK合并工站");
            this.treeView1.ExpandAll();//全展开

        }

        private void 读取参数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SystemCfg.ReadPointXmlToDGV(this.dgv_Points, SectionName);
            for (int i = 0; i < this.dgv_Points.Rows.Count; i++)
            {
                this.dgv_Points.Rows[i].Cells[5].Value = 10;//手动速度
            }
        }

        private void 保存参数ToolStripMenuItem_Click(object sender, EventArgs e)
        {        
                SystemCfg.WritePointDgvToXml(this.dgv_Points, SectionName);


            if (SectionName == "UpAndDownLoad")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.UpAndDownLoad, "UpAndDownLoad");
            }
            else if (SectionName == "MagSplit")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.MagSplit, "MagSplit");
            }
            else if (SectionName == "SlidesTransit")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.SlidesTransit, "SlidesTransit");
            }
            else if (SectionName == "TransitCarryLocation")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.TransitCarryLocation, "TransitCarryLocation");
            }
            else if (SectionName == "TransitPickUp")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.TransitPickUp, "TransitPickUp");
            }
            else if (SectionName == "FlyFilming1")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.FlyFilming1, "FlyFilming1");
            }
            else if (SectionName == "TransitCarry")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.TransitCarry, "TransitCarry");
            }
            else if (SectionName == "FlyFilming2")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.FlyFilming2, "FlyFilming2");
            }
            else if (SectionName == "FlyFilming3")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.FlyFilming3, "FlyFilming3");
            }
            else if (SectionName == "FlyAfterPickUp")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.FlyAfterPickUp, "FlyAfterPickUp");
            }
            else if (SectionName == "PutDownOKNG")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.PutDownOKNG, "PutDownOKNG");
            }
            else if (SectionName == "OKFull")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.OKFull, "OKFull");
            }
            else if (SectionName == "OKMagMerge")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.OKMagMerge, "OKMagMerge");
            }
        }

        private void 下载参数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (SectionName == "UpAndDownLoad")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.UpAndDownLoad, "UpAndDownLoad");
            }
            else if (SectionName == "MagSplit")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.MagSplit, "MagSplit");
            }
            else if (SectionName == "SlidesTransit")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.SlidesTransit, "SlidesTransit");
            }
            else if(SectionName == "TransitCarryLocation")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.TransitCarryLocation, "TransitCarryLocation");
            }
            else if (SectionName == "TransitPickUp")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.TransitPickUp, "TransitPickUp");
            }
            else if (SectionName == "FlyFilming1")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.FlyFilming1, "FlyFilming1");
            }
            else if (SectionName == "TransitCarry")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.TransitCarry, "TransitCarry");
            }
            else if (SectionName == "FlyFilming2")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.FlyFilming2, "FlyFilming2");
            }
            else if (SectionName == "FlyFilming3")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.FlyFilming3, "FlyFilming3");
            }
            else if (SectionName == "FlyAfterPickUp")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.FlyAfterPickUp, "FlyAfterPickUp");
            }
            else if (SectionName == "PutDownOKNG")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.PutDownOKNG, "PutDownOKNG");
            }
            else if (SectionName == "OKFull")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.OKFull, "OKFull");
            }
            else if (SectionName == "OKMagMerge")
            {
                SystemCfg.WritePointXmlToSystem(SystemCfg.OKMagMerge, "OKMagMerge");
            }
        }

        private void 新增参数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Device.OperLevel != Device.OperationLevel.Developer)
            {
                MessageBox.Show("非开发人员,请勿操作");
                return;
            }
            try
            {
                int row = dgv_Points.SelectedCells[0].RowIndex;// 行号 

                dgv_Points.Rows.Insert(row + 1);


            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void 删除参数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Device.OperLevel != Device.OperationLevel.Developer)
            {
                MessageBox.Show("非开发人员,请勿操作");
                return;
            }
            try
            {
                int row = dgv_Points.SelectedCells[0].RowIndex;// 行号 

                int col = dgv_Points.SelectedCells[0].ColumnIndex;// 列号   

                dgv_Points.Rows.Remove(dgv_Points.Rows[row]);//删除行

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void 定位该点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Device.StateMode == Device.STATEMode.RUN)
            {
                MessageBox.Show("设备正在自动运行,请勿点击");
                return;
            }
            try
            {
                int row = dgv_Points.SelectedCells[0].RowIndex;// 行号  

                int AxisNo = Convert.ToInt16(this.dgv_Points.Rows[row].Cells[2].Value.ToString());//轴号

                double Pos = Convert.ToDouble(this.dgv_Points.Rows[row].Cells[3].Value.ToString());//位置

                double Speed = Convert.ToDouble(this.dgv_Points.Rows[row].Cells[5].Value.ToString());//速度

                Motions.MotionMgr.GetInstance().Card.AbsMove(AxisNo, Pos, Speed);

                Motions.MotionMgr.GetInstance().WaitAbs(AxisNo, Pos);

                MessageBox.Show("定位完成");

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void 示教参数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                int row = dgv_Points.SelectedCells[0].RowIndex;// 列号

                int AxisNo = Convert.ToInt16(this.dgv_Points.Rows[row].Cells[2].Value.ToString());

                this.dgv_Points.Rows[row].Cells[3].Value = Motions.MotionMgr.GetInstance().Card.GetAxisActPos(AxisNo);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void 参数检查ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int AxisNo;//轴编号

            string AxisName = "";//轴名称

            for (int i = 0; i < this.dgv_Points.Rows.Count; i++)
            {
                //控件中轴名称
                AxisName = this.dgv_Points.Rows[i].Cells[1].Value.ToString();
                //控件中轴编号
                AxisNo = Convert.ToInt16(this.dgv_Points.Rows[i].Cells[2].Value.ToString());

                //配置参数中轴编号
                int no = Tool.SystemCfg.AxisName[AxisName].Index;
                //配置参数中轴名称
                string name = Tool.SystemCfg.AxisName[AxisName].Name;

                //名称与轴号不配
                if (AxisName != name || AxisNo != no)
                {
                    this.dgv_Points.Rows[i].DefaultCellStyle.BackColor = Color.Red;//轴名称与轴编号都对显示红色 轴名称与编号不一致             
                }
                else
                {
                    this.dgv_Points.Rows[i].DefaultCellStyle.BackColor = Color.Empty;//轴名称与轴编号都对显示红色 轴名称与编号不一致           
                }
            }
        }

        private void Frm_AxisPoint_FormClosing(object sender, FormClosingEventArgs e)
        {
            Frm_AxisPoint._instance = null;
        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode.Parent == null)
            {
                return;
            }

            string name = ((TreeView)sender).SelectedNode.Text.Trim();//子节点名称

            SectionName = "";

            if (name == "弹夹搬运工站") SectionName = "UpAndDownLoad";
            if (name == "胶板拆分工站") SectionName = "MagSplit";
            if (name == "胶板中转工站") SectionName = "SlidesTransit";
            if (name == "中转定位工站") SectionName = "TransitCarryLocation";
            if (name == "中转定位取料工站") SectionName = "TransitPickUp";
            if (name == "飞拍1工站") SectionName = "FlyFilming1";
            if (name == "中转搬运工站") SectionName = "TransitCarry";
            if (name == "双工位飞拍2工站") SectionName = "FlyFilming2";
            if (name == "双工位飞拍3工站") SectionName = "FlyFilming3";
            if (name == "飞拍后取料工站") SectionName = "FlyAfterPickUp";
            if (name == "分OKNG工站") SectionName = "PutDownOKNG";
            if (name == "OK放料工站") SectionName = "OKFull";
            if (name == "OK合并工站") SectionName = "OKMagMerge";

            if (SectionName == "")
            {
                MessageBox.Show("站位名称加载异常");
                return;
            }





            //把XML文件中的参数读取到DataGridView中
            SystemCfg.ReadPointXmlToDGV(this.dgv_Points, SectionName);

            for (int i = 0; i < this.dgv_Points.Rows.Count; i++)
            {
                this.dgv_Points.Rows[i].Cells[5].Value = 100;//手动速度
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < dgv_Points.Rows.Count; i++)
                {
                    int AxisNo = Convert.ToInt16(this.dgv_Points.Rows[i].Cells[2].Value.ToString());

                    double PosValue = Convert.ToDouble(this.dgv_Points.Rows[i].Cells[3].Value.ToString());//控件中的位置

                    double PosActValue = Motions.MotionMgr.GetInstance().Card.GetAxisActPos(AxisNo);//实际位置

                    if ((PosValue - PosActValue) >= -0.5 && (PosValue - PosActValue) <= 0.5)
                    {
                        Tool.CtrDisplay.ShowText(this.dgv_Points, i, 6, "True", Color.Green, Color.Empty);
                    }
                    else
                    {
                        Tool.CtrDisplay.ShowText(this.dgv_Points, i, 6, "False", Color.White, Color.Empty);
                    }
                }
            }
            catch (Exception)
            {

            }
            
        }

        
    }
}